package com.bdqn.zhang.servlet;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.zhang.pojo.*;
import com.bdqn.zhang.service.ServiceMemberinfo;
import com.bdqn.zhang.service.impl.ServiceMemberinfoimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet(name = "servletMembercx", urlPatterns = {"/member"})
//文件上传
@MultipartConfig
public class ServletMembercx extends HttpServlet {
    ServiceMemberinfo serviceMemberinfo = new ServiceMemberinfoimpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理post传参乱码
        req.setCharacterEncoding("UTF-8");
        //处理响应乱码
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //opr 判断不同的业务类型
        String opr = req.getParameter("opr");
        //equals判断常量在前 不会出现空指针异常
        boolean flag;
        if ("getAll".equals(opr)) {
            //获取当前页
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            //全查
            List<Memberinfo> all = serviceMemberinfo.getAll((int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
//            //获取表中所以的记录数
            int count = serviceMemberinfo.getCount();
            LayData laydata = new LayData();
            laydata.setCode(0);
            laydata.setMsg("");
            laydata.setCount(count);
            laydata.setData(all);
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);

        } else if ("getAll2".equals(opr)) {
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            LayData laydata = new LayData();
            String p_name = req.getParameter("p_name");
            if (p_name != null && !" ".equals(p_name)) {
                List<Memberinfo> mh = serviceMemberinfo.dan(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = serviceMemberinfo.mhdan(p_name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
            }
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("del".equals(opr)) {
            int i = 0;
            String id = req.getParameter("id");
            String[] ids = id.split(",");  //分割id，成一个字符串数组
            int n = ids.length;
            for (int j = 0; j < n; j++) {
                i = serviceMemberinfo.del(ids[j]);       //循环执行删除
            }
            flag = false;
            if (i > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));


            // String id = req.getParameter("id");

        } else if ("edit".equals(opr)) {
            String id = req.getParameter("id");
            Memberinfo memberinfo = serviceMemberinfo.getStudentByid(id);
            //讲Memberinfo存储到作用域中
            req.getSession().setAttribute("memberinfo", memberinfo);
            //页面进行跳转
            resp.sendRedirect("cs.jsp");
        } else if ("upload".equals(opr)) {
            String realPath = req.getSession().getServletContext().getRealPath("static");
            Part part = req.getPart("file");
            String fileName = part.getSubmittedFileName();
            AjaxObject object = new AjaxObject();
            object.setImgSrc(fileName);
            resp.getWriter().write(JSONObject.toJSONString(object));
        } else if ("addSave".equals(opr)) {
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String email = req.getParameter("email");
            String phone = req.getParameter("phone");
            String address = req.getParameter("address");
            String sex = req.getParameter("sex");
            String zp = req.getParameter("photo");
            String xb;
            if ("1".equals(sex)) {
                xb = "男";
            } else {
                xb = "女";
            }
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String dates = df.format(date);
            // Formats a Date into a date/time string.// 2019-07-03
            String photo = "static/img/" + zp;
            int zhuangtai = 1;
            int count = serviceMemberinfo.add(new Memberinfo(password,email, name, phone, dates, address, xb, photo, zhuangtai));//匿名对象 调用完之后会被内存回收 省内存空间
            flag = false;
            if (count > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        } else if ("checkName".equals(opr)) {
            String name = req.getParameter("name");
            int count = serviceMemberinfo.checkNameUnique(name);
            flag = false;
            if (count > 0) {
                flag = true;
            }
            String jsonString = JSONObject.toJSONString(flag);
            resp.getWriter().write(jsonString);

        } else if ("checkNameemail".equals(opr)) {
            String name = req.getParameter("name");
            int count = serviceMemberinfo.emails(name);
            flag = false;
            if (count > 0) {
                flag = true;
            }
            String jsonString = JSONObject.toJSONString(flag);
            resp.getWriter().write(jsonString);

        } else if ("addSave2".equals(opr)) {
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String email = req.getParameter("email");
            String phone = req.getParameter("phone");
            String address = req.getParameter("address");
            String sex = req.getParameter("sex");
            String zp = req.getParameter("photo");
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String dates = df.format(date);
            // Formats a Date into a date/time string.// 2019-07-03
            String photo = "static/img/" + zp;
            String zhuang = req.getParameter("zhuangtai");
            int zhuangtai = Integer.parseInt(zhuang);
            int count = serviceMemberinfo.edits(new Memberinfo(email, name, phone, dates, address, sex, photo, zhuangtai,id));//匿名对象 调用完之后会被内存回收 省内存空间
            flag = false;
            if (count > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        } else if ("ztxg".equals(opr)) {
            String id = req.getParameter("id");
            String zhi = req.getParameter("zhi");
            int zhis = Integer.parseInt(zhi);
            int zt;
            if (zhis == 0) {
                zt = 1;
            } else {
                zt = 0;
            }
            int count = serviceMemberinfo.gai(new Memberinfo(id, zt));//匿名对象 调用完之后会被内存回收 省内存空间
            flag = false;
            if (count > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        } else if ("ztcx".equals(opr)) {
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            LayData laydata = new LayData();
            String p_name = req.getParameter("p_name");
            int name;
            if (p_name != null && !" ".equals(p_name)) {
                if ("启用".equals(p_name)) {
                    name = 1;
                } else {
                    name = 0;
                }
                List<Memberinfo> mh = serviceMemberinfo.ztcx(name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = serviceMemberinfo.mhztcx(name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
            }
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("yhmcx".equals(opr)) {
            String p_name = req.getParameter("p_name");
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            String rr = req.getParameter("rr");
            LayData laydata = new LayData();
            if ("仅查询用户名".equals(rr)) {
                List<Memberinfo> mh = serviceMemberinfo.yhmcx(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = serviceMemberinfo.mhdan(p_name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            } else if ("仅查询手机号".equals(rr)) {
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(serviceMemberinfo.mhdan(p_name));
                laydata.setData(serviceMemberinfo.sjhcx(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit)));
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            } else if ("仅查询性别".equals(rr)) {
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(serviceMemberinfo.mhdan(p_name));
                laydata.setData(serviceMemberinfo.xbcx(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit)));
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            } else if ("仅查询邮箱".equals(rr)) {
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(serviceMemberinfo.mhdan(p_name));
                laydata.setData(serviceMemberinfo.yxcx(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit)));
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            }
        }else if ("indexyh".equals(opr)) {
            int count = serviceMemberinfo.getCount();
            String jsonString = JSONObject.toJSONString(count);
            resp.getWriter().write(jsonString);
        }else if ("tj".equals(opr)) {
            List<Memberinfo> all = serviceMemberinfo.xb();
            String data = JSONObject.toJSONString(all);
            resp.getWriter().write(data);
        }
    }
}
